package com.zyd.controller.role;

import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.RoleEntity;
import com.zyd.io.vo.res.ResBody;
import com.zyd.io.page.PageBean;
import com.zyd.io.vo.role.RoleVO;
import com.zyd.service.role.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roles")
public class RoleController {

    private final static Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService service;

    @PostMapping(value = "/addRole", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody addRole(@RequestBody RoleVO roleVO) {
        try {
            RoleEntity role = new RoleEntity();
            role.setCode(roleVO.getCode());
            role.setName(roleVO.getName());
            
            service.addRole(role);
            return new ResBody(ErrorDef.SUCCESS, "新增角色成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "新增角色失败");
        }
    }

    @PostMapping(value = "/updateRole", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody updateRole(@RequestBody RoleVO roleVO) {
        try {
            RoleEntity role = new RoleEntity();
            role.setId(roleVO.getId());
            role.setName(roleVO.getName());
            role.setCode(roleVO.getCode());
            service.updateRole(role);
            return new ResBody(ErrorDef.SUCCESS, "修改角色成功");
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "修改角色失败");
        }
    }

    @PostMapping(value = "deleteRole", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody deleteRole(@RequestBody Map<String, Object> request) {
        try {
            service.deleteRole((String) request.get("id"));
            return new ResBody(ErrorDef.SUCCESS, "删除角色成功");
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "删除角色失败");
        }
    }

    @PostMapping(value = "/roleList", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findRoleList(@RequestBody Map<String, Object> request) {
        try {
            PageBean<RoleEntity> roleList = service.findRoleList((String) request.get("name"),
                    (int)request.get("currentPage"), (int)request.get("pageSize"));
            List<RoleVO> ret = new ArrayList<>();
            for (RoleEntity role : roleList.getItems()) {
                RoleVO roleVO = new RoleVO();
                roleVO.setId(role.getId());
                roleVO.setCode(role.getCode());
                roleVO.setName(role.getName());
                ret.add(roleVO);
            }
            PageBean<RoleVO> page = new PageBean<>(roleList.getCurrentPage(), roleList.getPageSize(), roleList.getTotalNum());
            page.setItems(ret);

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询角色列表成功");
            resBody.setData(page);
            return resBody;
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询角色列表失败");
        }
    }

    @PostMapping(value = "findRole", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findRole(@RequestBody Map<String, Object> request) {
        try {
            RoleEntity role = service.findRole((String) request.get("id"));
            RoleVO roleVO = new RoleVO();
            roleVO.setId(role.getId());
            roleVO.setName(role.getName());
            roleVO.setCode(role.getCode());
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询角色成功");
            resBody.setData(roleVO);
            return resBody;
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询角色失败");
        }
    }

}
